<%@ page import="java.util.List" %>
<%@ page import="com.example.demo_text.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    // 获取课程 ID 和名称
    String courseId = request.getParameter("course_id");
    
    // 调试输出
    System.out.println("discussions.jsp - 接收到的courseId: " + courseId);
    
    String courseName = "";
    if (courseId != null && !courseId.isEmpty()) {
        courseName = CourseDAO.getCourseNameById(courseId);
    }

    // 获取错误信息
    String error = request.getParameter("error");

    // 获取讨论列表
    List<Question> discussions = QuestionDAO.getQuestionsByCourseId(courseId);
%>

<html>
<head>
    <title>讨论区</title>
    <style>
        /* 全局样式 */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #e0f7fa, #f3e5f5);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .card {
            background: white;
            width: 90%;
            max-width: 800px;
            padding: 30px 25px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #374151;
        }

        h3 {
            margin-bottom: 15px;
            color: #374151;
        }

        p {
            text-align: center;
            color: #4b5563;
            margin-bottom: 20px;
        }

        /* 讨论表单样式 */
        .discussion-form {
            margin: 20px 0;
            padding: 20px;
            background-color: #f9fafb;
            border-radius: 8px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #d1d5db;
            border-radius: 6px;
            height: 100px;
            resize: vertical;
        }

        button {
            background-color: #3b82f6;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #2563eb;
        }

        /* 讨论列表样式 */
        .discussion-list {
            margin-top: 20px;
            max-height: 500px;
            overflow-y: auto;
        }

        .discussion-item {
            background-color: #f9fafb;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
        }

        .discussion-content {
            font-size: 15px;
            color: #374151;
        }

        .answer-item {
            margin-left: 20px;
            border-left: 3px solid #3b82f6;
            padding-left: 10px;
            margin-top: 10px;
        }

        .comment-item {
            margin-left: 30px;
            border-left: 2px solid #10b981;
            padding-left: 8px;
            margin-top: 5px;
            font-size: 13px;
            color: #6b7280;
        }

        .comment-form {
            margin-left: 30px;
            margin-top: 5px;
        }

        .comment-form textarea {
            width: calc(100% - 30px);
            padding: 6px;
            font-size: 13px;
            height: 60px;
            resize: vertical;
        }

        .comment-form button {
            background-color: #10b981;
            color: white;
            border: none;
            padding: 5px 10px;
            font-size: 13px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 5px;
        }

        .comment-toggle {
            font-size: 12px;
            color: #6b7280;
            background: none;
            border: none;
            cursor: pointer;
            text-decoration: underline;
            padding: 0;
            margin-top: 3px;
        }
    </style>
</head>
<body>

<div class="card">
    <h2>讨论区</h2>
    <% if (!courseName.isEmpty()) { %>
    <p>当前课程名称：<%= courseName %></p>
    <% } else { %>
    <p>未找到课程信息或未指定课程 ID。</p>
    <% } %>

    <!-- 错误提示 -->
    <% if (error != null && !error.isEmpty()) { %>
    <p style="color:red; text-align:center; margin-bottom: 20px;"><%= error %></p>
    <% } %>

    <!-- 发布讨论表单 -->
    <div class="discussion-form">
        <h3>提出问题</h3>
        <form action="SubmitQuestionServlet" method="post">
            <input type="hidden" name="course_id" value="<%= courseId %>"/>
            
            <div class="form-group">
                <label for="question_content">问题内容：</label>
                <textarea id="question_content" name="question_content" required></textarea>
            </div>
            
            <button type="submit">发布问题</button>
        </form>
    </div>

    <!-- 讨论列表 -->
    <div class="module-content">
        <h3>最新讨论</h3>
        <div class="discussion-list">
            <% if (!discussions.isEmpty()) { %>
                <% for (Question discussion : discussions) { %>
                <div class="discussion-item">
                    <!-- 显示问题内容 -->
                    <div class="discussion-content"><%= discussion.getQuestionContent() %></div>

                    <!-- 显示已有回答 -->
                    <%
                        List<Answer> answers = AnswerDAO.getAnswersByQuestionId(discussion.getQuestionId());
                        if (!answers.isEmpty()) {
                            for (Answer ans : answers) {
                    %>
                    <div class="answer-item">
                        <div style="font-size: 14px; color: #4b5563;"><%= ans.getAnswerContent() %></div>
                        <small style="color: #9ca3af;"><%= ans.getAnswerTime() %></small>
                        
                        <!-- 显示评论 -->
                        <% 
                            List<Comment> comments = CommentDAO.getCommentsByAnswerId(ans.getAnsId()); 
                            if (comments != null && !comments.isEmpty()) {
                                for (Comment comment : comments) {
                        %>
                        <div class="comment-item">
                            <div><%= comment.getCommentContent() %></div>
                            <small style="color: #9ca3af;"><%= comment.getCommentTime() %></small>
                        </div>
                        <% 
                                }
                            } 
                        %>
                        
                        <!-- 添加评论按钮，默认隐藏评论表单 -->
                        <button class="comment-toggle" onclick="toggleCommentForm('<%= ans.getAnsId() %>')">添加评论</button>
                        
                        <!-- 评论表单 -->
                        <div id="comment-form-<%= ans.getAnsId() %>" class="comment-form" style="display: none;">
                            <form action="SubmitCommentServlet" method="post">
                                <input type="hidden" name="answer_id" value="<%= ans.getAnsId() %>" />
                                <input type="hidden" name="course_id" value="<%= courseId %>" />
                                <textarea name="comment_content" placeholder="添加评论..." required></textarea>
                                <button type="submit">提交评论</button>
                            </form>
                        </div>
                    </div>
                    <%
                            }
                        }
                    %>

                    <!-- 回答表单 -->
                    <form action="SubmitAnswerServlet" method="post" style="margin-top: 15px;">
                        <input type="hidden" name="question_id" value="<%= discussion.getQuestionId() %>" />
                        <input type="hidden" name="course_id" value="<%= courseId %>" />

                        <div class="form-group">
                            <label for="answer_<%= discussion.getQuestionId() %>">你的回答：</label>
                            <textarea id="answer_<%= discussion.getQuestionId() %>" 
                                    name="answer_content" 
                                    placeholder="写下你的回答..." 
                                    required
                                    style="height: 80px;"></textarea>
                        </div>

                        <button type="submit" style="background-color: #10b981;">提交回答</button>
                    </form>
                </div>
                <% } %>
            <% } else { %>
            <p>暂无讨论内容。</p>
            <% } %>
        </div>
    </div>
</div>

<script>
    function toggleCommentForm(answerId) {
        const formId = 'comment-form-' + answerId;
        const form = document.getElementById(formId);
        if (form.style.display === 'none') {
            form.style.display = 'block';
        } else {
            form.style.display = 'none';
        }
    }
</script>

</body>
</html>
